package com.jt.safe.ui.activity


import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.dueeeke.videoplayer.ijk.IjkPlayer
import com.dueeeke.videoplayer.player.AbstractPlayer
import com.dueeeke.videoplayer.player.VideoView
import com.jt.safe.R
import com.jt.safe.base.BaseActivity
import com.jt.safe.utils.video.Utils
import com.jt.safe.utils.video.cache.PreloadManager
import com.jt.safe.viewmodel.NullViewModel
import com.jt.safe.widget.TikTokController
import kotlinx.android.synthetic.main.activity_video_play.*

class VideoPlayActivity: BaseActivity<NullViewModel>() {

    val video_url ="https://aweme.snssdk.com/aweme/v1/play/?video_id=97022dc18711411ead17e8dcb75bccd2&line=0&ratio=720p&media_type=4&vr_type=0"
    val image_url ="https://p9.pstatp.com/large/4c87000639ab0f21c285.jpeg"
    val mPreloadManager   by lazy{ PreloadManager.getInstance(this) }

    override val layoutId: Int
        get() = R.layout.activity_video_play

    override fun initView() {
        val tv_title = tikTok_View.findViewById<TextView>(R.id.tv_title)
        val iv_thumb = tikTok_View.findViewById<ImageView>(R.id.iv_thumb)
        Glide.with(this)
            .load(image_url)
            .placeholder(android.R.color.white)
            .into(iv_thumb)
        mPreloadManager.addPreloadTask(video_url, 0)

        val videoView = VideoView<IjkPlayer>(this)
        videoView.setLooping(true)
        videoView.setScreenScaleType(VideoView.SCREEN_SCALE_CENTER_CROP);

        val mController = TikTokController(this);
        videoView.setVideoController(mController);

        Utils.removeViewFormParent(videoView);
        videoView.setUrl(video_url)
        mController.addControlComponent(tikTok_View, true)
        container.addView(videoView, 0)
        videoView.start()
    }

    override fun createObserver() {

    }

    override fun onDestroy() {
        super.onDestroy()
        mPreloadManager.removePreloadTask(video_url)
    }
}
